package v4.text;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;
import java.util.Random;

public class TextJmConfig implements Config {
	
	private int TEXT_R_LEN = 10;
	private int AES_KEYSIZE = AesKit.AES_KEYSIZE_128;//192,256
	private String AES_PADDING = AesKit.AES_CIPHER_ALGORITHM;
	private String RSA_PUBLICKEY = RsaKit.DEFAULT_PUBLIC_KEY_STRING;
	private String RSA_PRIVATEKEY = RsaKit.DEFAULT_PRIVATE_KEY_STRING;
	private boolean USE_TOTP = true;
	
	public TextJmConfig() {
		
	}
	
	public static TextJmConfig copy(TextJmConfig config) {
		TextJmConfig newConfig = new TextJmConfig();
		newConfig.TEXT_R_LEN = config.TEXT_R_LEN;
		newConfig.AES_KEYSIZE = config.AES_KEYSIZE;
		newConfig.AES_PADDING = config.AES_PADDING;
		newConfig.RSA_PUBLICKEY = config.RSA_PUBLICKEY;
		newConfig.RSA_PRIVATEKEY = config.RSA_PRIVATEKEY;
		newConfig.USE_TOTP = config.USE_TOTP;
		return newConfig;
	}
	
	public static Properties loadConfig(String file) {
		return loadConfig(new File(file));
	}
	
	public static Properties loadConfig(File file) {
		Properties config = new Properties();
		if(file!=null && file.exists()) {
			try {
				config.load(new FileInputStream(file));
			} catch (FileNotFoundException e) {
				e.printStackTrace();
				throw new RuntimeException(e.getMessage());
			} catch (IOException e) {
				e.printStackTrace();
				throw new RuntimeException(e.getMessage());
			}
		}
		return config;
	}
	
	@Override
	public void config(Properties config) {
		String text_r_len = config.getProperty("TEXT_R_LEN");
		if(text_r_len!=null) {
			TEXT_R_LEN = Integer.parseInt(text_r_len);
			if(TEXT_R_LEN < 4 || TEXT_R_LEN > 16) {
				throw new RuntimeException("TEXT_R_LEN must in [4, 16]");
			}
		}
		String aes_keysize = config.getProperty("AES_KEYSIZE");
		if(aes_keysize!=null) {
			AES_KEYSIZE = Integer.parseInt(aes_keysize);
		}
		String aes_padding = config.getProperty("AES_PADDING");
		if(aes_padding!=null) {
			AES_PADDING = aes_padding;
		}
		String use_totp = config.getProperty("USE_TOTP");
		if(use_totp!=null) {
			USE_TOTP = "true".equals(use_totp) || "1".equals(use_totp);
		}
		String rsa_publickey_file = config.getProperty("RSA_PUBLICKEY_FILE");
		if(rsa_publickey_file!=null) {
			RSA_PUBLICKEY = FileTxtKit.getTxtKey(new File(rsa_publickey_file));
		}
		String rsa_publickey = config.getProperty("RSA_PUBLICKEY");
		if(rsa_publickey!=null) {
			RSA_PUBLICKEY = rsa_publickey;
		}
		String rsa_privatekey_file = config.getProperty("RSA_PRIVATEKEY_FILE");
		if(rsa_privatekey_file!=null) {
			RSA_PRIVATEKEY = FileTxtKit.getTxtKey(new File(rsa_privatekey_file));
		}
		String rsa_privatekey = config.getProperty("RSA_PRIVATEKEY");
		if(rsa_privatekey!=null) {
			RSA_PRIVATEKEY = rsa_privatekey;
		}
	}
	
	public int getTEXT_R_LEN() {
		return TEXT_R_LEN;
	}
	public void setTEXT_R_LEN(int tEXT_R_LEN) {
		TEXT_R_LEN = tEXT_R_LEN;
	}
	public int getAES_KEYSIZE() {
		return AES_KEYSIZE;
	}
	public void setAES_KEYSIZE(int aES_KEYSIZE) {
		AES_KEYSIZE = aES_KEYSIZE;
	}
	public String getAES_PADDING() {
		return AES_PADDING;
	}
	public void setAES_PADDING(String aES_PADDING) {
		AES_PADDING = aES_PADDING;
	}
	public String getRSA_PUBLICKEY() {
		return RSA_PUBLICKEY;
	}
	public void setRSA_PUBLICKEY(String rSA_PUBLICKEY) {
		RSA_PUBLICKEY = rSA_PUBLICKEY;
	}
	public String getRSA_PRIVATEKEY() {
		return RSA_PRIVATEKEY;
	}
	public void setRSA_PRIVATEKEY(String rSA_PRIVATEKEY) {
		RSA_PRIVATEKEY = rSA_PRIVATEKEY;
	}
	public boolean isUSE_TOTP() {
		return USE_TOTP;
	}
	public void setUSE_TOTP(boolean uSE_TOTP) {
		USE_TOTP = uSE_TOTP;
	}

	@Override
	public int rX() {
		//4333-6666
		return new Random().nextInt(2334) + 4333;
	}

	@Override
	public int rY() {
		//1000-3333
		return new Random().nextInt(2334) + 1000;
	}

	@Override
	public String rR(int n) {
		return r(n);
	}
	
	public static String r(int n) {
		StringBuilder sb = new StringBuilder();
		for(int i=0; i<n; i++) {
			sb.append(rRs[new Random().nextInt(rRs.length)]);
		}
		return sb.toString();
	}

}
